<?php

/**
 * Holiday models
 *
 * Holiday models in data
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

/**
 * Class Holiday
 *
 * Class Holiday in data models
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Data_Calendar_Holiday extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'wt_data_holiday';
    public static $key = 'wdh_id';
    public static $rules = array(
                                'wdh_hol_name' => 'required|Unique:wt_data_holiday',
                                'wdh_hol_date' => 'required|Unique:wt_data_holiday',
                            );
							
    /**
     * Function validate that pass parameter
     *
     * @param varchar $data the varchar to quote
	 *
	 * @return parameter.
     */
    public static function validate($data)
	{
        return Validator::make($data, Static::$rules);
    }
	
	/**
     * Function listHoliday
	 *
	 * @return listHoliday.
     */
    public static function listHoliday()
	{
        $allFlow = Data_Calendar_Holiday::order_by('wdh_hol_date','ASC')
			->paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
        $datagrid->setFields(array('wdh_hol_name' => Str::upper(Lang::line('admin.holidaylist')->get())));
        $datagrid->setDate(array('wdh_hol_date' => Str::upper(Lang::line('global.date')->get())));
        $datagrid->setAction(Lang::line('global.edit')->get(), 'editHol', true, array('wdh_id'));
        $datagrid->setAction(Lang::line('global.delete')->get(), 'deleteHol', true, array('wdh_id', 'wdh_hol_name'));
        $datagrid->setTable('holiday', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->showNumbering(true);
        $datagrid->build($allFlow, 'wdh_id');

        return $datagrid->render();
    }
	
	/**
     * Function addHoliday that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function addHoliday($input)
	{
        if ($input['wdh_id'] == null) {
            $holiday = new Data_Calendar_Holiday;
            $action = Lang::line('global.add')->get();
        } else {
            $holiday = Data_Calendar_Holiday::find($input['wdh_id']);
            $action = Lang::line('global.edit')->get();
        }
        
        $holiday->wdh_hol_name = Str::title($input['wdh_hol_name']);
        $holiday->wdh_hol_desc = $input['wdh_hol_desc'];
        $holiday->wdh_hol_date = date("Y-m-d", strtotime($input['wdh_hol_date']));
        $holiday->save();

        Log::write(
			Lang::line('admin.calendar')->get(), 
			$action .' <strong>'. $input['wdh_hol_name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.calendar')->get(), 
			Auth::user()->su_id, 
			$action .' <strong>'. $input['wdh_hol_name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }
	
	public static function editHoliday($input)
	{
		$holiday = Data_Calendar_Holiday::find($input['wdh_id']);
		$action = Lang::line('global.edit')->get();
        
        $holiday->wdh_hol_name = Str::title($input['wdh_hol_name']);
        $holiday->wdh_hol_desc = $input['wdh_hol_desc'];
        $holiday->wdh_hol_date = date("Y-m-d", strtotime($input['wdh_hol_date']));
        $holiday->save();

        Log::write(
			Lang::line('admin.calendar')->get(), 
			$action .' <strong>'. $input['wdh_hol_name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.calendar')->get(), 
			Auth::user()->su_id, 
			$action .' <strong>'. $input['wdh_hol_name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }
	
	/**
     * Function holiday that pass parameter
     *
     * @param varchar $month the varchar to quote
     * @param varchar $year  the varchar to quote
	 *
	 * @return parameter.
     */
    public static function holiday($month, $year)
	{
		$holiday = Data_Calendar_Holiday::all();

		$data = array();

		foreach ($holiday as $value) {

			$dbDate = date('j', strtotime($value->wdh_hol_date));
			$dbMonth = date('n', strtotime($value->wdh_hol_date));
			$dbYear = date('Y', strtotime($value->wdh_hol_date));
			
			if ($dbMonth == $month && $dbYear == $year) {
				$data[$dbDate][$dbMonth][$dbYear]['leave'] = Str::title($value->wdh_hol_name);
			}
			
		}

		return $data;
    }
	
	/**
     * Function holidayData that pass parameter
     *
     * @param integer $id the integer to quote
	 *
	 * @return parameter.
     */
    public static function holidayData($id)
	{
        $data = array();
        $raw = Data_Calendar_Holiday::find($id);
        $data['wdh_id'] = $raw->wdh_id;
        $data['wdh_hol_name'] = $raw->wdh_hol_name;
        $data['wdh_hol_desc'] = $raw->wdh_hol_desc;
        $data['wdh_hol_date'] = date('d-m-Y', strtotime($raw->wdh_hol_date));

        return $data;
    }
	
	/**
     * Function deleteHol that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function deleteHol($input)
	{
        Data_Calendar_Holiday::find($input['id'])->delete();

        Log::write(
			Lang::line('admin.calendar')->get(), 
			Lang::line('global.delete')->get().' <strong>'. $input['name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.calendar')->get(), 
			Auth::user()->su_id, 
			Lang::line('global.delete')->get().' <strong>'. $input['name'] .'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }

}

?>